package Controller;

import Model.ChoreBean;
import Model.ChoreListBean;
import Model.UserBean;
import dao.ChoreListDAO;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.inject.Named;

/**
 *
 * @author wrjohns
 */

@Named(value = "choreListController")
@SessionScoped
public class ChoreListController implements Serializable
{
     private ChoreListBean choreListBean;

    public ChoreListBean getChoreListBean() {
        return choreListBean;
    }

    public void setChoreListBean(ChoreListBean choreListBean) {
        this.choreListBean = choreListBean;
    }
    

    
    public List<SelectItem> getChoreList()
    {
       UserBean user = (UserBean) FacesContext.getCurrentInstance().
                    getExternalContext().getSessionMap().get("userBean");
       List<ChoreBean> chores = null;
       ArrayList<SelectItem> choreNames = null;
        try 
        {
            ChoreListDAO.init();
            chores = ChoreListDAO.getChores(user.getHouseName());
            ChoreListDAO.terminate();
            choreNames = new ArrayList<SelectItem>();
            for(int i = 0; i < chores.size(); i++)
            {
                choreNames.add(new SelectItem(chores.get(i).getChoreName()));
            }
            //choreListBean.setList(choreNames);
        } 
        catch (SQLException ex) 
        {
            Logger.getLogger(HouseholdListController.class.getName()).log(Level.SEVERE, null, ex);
        }   
       
        return choreNames;
    }
    
    public List<ChoreBean> getChoreList(UserBean user)
    {
        List<ChoreBean> ret = null;
        try 
        {
             ChoreListDAO.init();
             ret = ChoreListDAO.getChores(user.getHouseName());
             ChoreListDAO.terminate();
        } 
        catch (SQLException ex) 
        {
            Logger.getLogger(HouseholdListController.class.getName()).log(Level.SEVERE, null, ex);
        }   
        ArrayList<String> choreNames = new ArrayList<String>();
        for(int i = 0; i < ret.size(); i++)
        {
            choreNames.add(ret.get(i).getChoreName());
        }
        choreListBean.setList(choreNames);
        return ret;
    }
    
    public List<SelectItem> getDailyChores()
    {
        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int month = cal.get(Calendar.DAY_OF_MONTH);
        int year = cal.get(Calendar.DAY_OF_MONTH);
        UserBean user = (UserBean) FacesContext.getCurrentInstance().
                    getExternalContext().getSessionMap().get("userBean");
        String housename = user.getHouseName();
        
        List<ChoreBean> ret = null;
        try
        {
            ChoreListDAO.init();
            ret = ChoreListDAO.getChores(day, month, year, housename);
            
            List<ChoreBean> weeklyChores = ChoreListDAO.getWeeklyChores(housename);
            
            int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
            System.out.println("" + + cal.get(Calendar.DAY_OF_MONTH) + "\n" + cal.get(Calendar.MONTH) + "\n" + cal.get(Calendar.YEAR));
            for(int i = 0; i < weeklyChores.size(); i++)
            {
                cal.set(weeklyChores.get(i).getStartYear(), weeklyChores.get(i).getStartMonth(), weeklyChores.get(i).getStartDay());
                int newDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
                System.out.println("" + cal.get(Calendar.DAY_OF_MONTH) + "\n" + cal.get(Calendar.MONTH) + "\n" + cal.get(Calendar.YEAR));
                if(newDayOfWeek == dayOfWeek)
                {
                    ret.add(weeklyChores.get(i));
                }
            }
            
            ChoreListDAO.terminate();
        }
        catch(SQLException e)
        {
            Logger.getLogger(HouseholdListController.class.getName()).log(Level.SEVERE, null, e);
        }
        
        ArrayList<SelectItem> select = new ArrayList<SelectItem>();
        for(int i = 0; i < ret.size(); i++)
        {
            select.add(new SelectItem(ret.get(i).getChoreName()));
        }
        return select;
    }
}
